﻿using System;
using System.Linq;
using LDSCore;

namespace LDSModel.Dal
{
    public static class LogService
    {
        /// <summary>
        /// 记录操作日志
        /// </summary>
        /// <param name="type">操作类型</param>
        /// <param name="userId">用户Id</param>
        /// <param name="message">操作信息</param>
        public static void Operation(OpType type, Guid userId, string message)
        {
            var context = new DatabaseEntities();

            var operation = new Log_Operation
                {
                    OperatId = Guid.NewGuid(),
                    OpType = (short)type,
                    UserID = userId,
                    OpMessage = message,
                    OpTime = DateTime.Now
                };

            context.AddToLog_Operation(operation);
            context.SaveChanges();
        }

        /// <summary>
        /// 记录操作日志
        /// </summary>
        /// <param name="type">操作类型</param>
        /// <param name="userId">用户Id</param>
        /// <param name="message">操作信息</param>
        public static void OperationLog(OpType type, Guid userId, string message)
        {
            try
            {
                var operation = new Log_Operation
                    {
                        OperatId = Guid.NewGuid(),
                        OpType = (short)type,
                        UserID = userId,
                        OpMessage = message,
                        OpTime = DateTime.Now
                    };

                var context = new DatabaseEntities();

                context.AddToLog_Operation(operation);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                LogFileHelper.Record(ex);
            }
        }

        public static IQueryable QueryOpLog(OpType type, DateTime startTime, DateTime endTime)
        {
            endTime = endTime.AddDays(1).AddSeconds(-1);

            var context = new DatabaseEntities();

            var result = from o in context.Log_Operation
                         from u in context.Sys_User
                         where o.UserID == u.UserId && o.OpType == (int)type
                         && o.OpTime >= startTime && o.OpTime < endTime
                         orderby o.OpTime descending
                         select new
                         {
                             u.UserName,
                             Message = o.OpMessage,
                             OPTime = o.OpTime
                         };

            return result;
        }

        public static IQueryable QueryErrLog(DateTime startTime, DateTime endTime)
        {
            endTime = endTime.AddDays(1).AddSeconds(-1);

            var context = new DatabaseEntities();

            var result = from o in context.Log_Error
                         where o.CreateTime >= startTime && o.CreateTime < endTime
                         orderby o.CreateTime descending
                         select new
                         {
                             Description = o.Despriction,
                             o.CreateTime
                         };

            return result;
        }
    }
}
